import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';

class BaseRequestFormInputArea extends StatefulWidget {

  final BaseHttpRequestModel request;

  final bool isSaveASingle;

  final RequestSaveData requestSaveData;


  const BaseRequestFormInputArea({Key key, @required this.request, this.isSaveASingle = false, this.requestSaveData}) : super(key: key);

  @override
  _BaseRequestFormInputAreaState createState() => _BaseRequestFormInputAreaState();

}

class _BaseRequestFormInputAreaState extends State<BaseRequestFormInputArea> {

  BaseRequestFormProvider _baseRequestFormProvider;

  @override
  Widget build(BuildContext context) {
    _baseRequestFormProvider = BaseRequestFormProvider.of(context);
    var _date = _baseRequestFormProvider.getDataSource(widget.request);
    var valueStirng;
    if (_date != null && (_date is Map) && _date.length != 0) {
      (_date as Map).forEach((key, value) {
        valueStirng = value;
      });
    } else {
      valueStirng = _date;
    }
    return BaseRequestFormContainer(
      request: widget.request,
      child: BaseTextArea(
        isCount: false,
        isBorder: false,
        value: valueStirng ?? '',
        readOnly: _baseRequestFormProvider.isReadonly(widget.request),
        hintText: widget.request.notes,
        onEditingComplete: (String value) {
          if (value.isEmpty) {
            BaseUtils.showToast("${widget.request.name}不能为空");
          }
          if (widget.isSaveASingle && widget.isSaveASingle != null && value.isNotEmpty) {
            _baseRequestFormProvider.setDataSource(widget.request, value);
            widget.requestSaveData(widget.request, _baseRequestFormProvider.getMapDataSource(widget.request));
          }
        },
        onChanged: (String value) {
          /// 保存值
          _baseRequestFormProvider.setDataSource(widget.request, value);
        },
      ),
    );
  }

}
